package com.handinfo.android.ui;

import java.io.DataInputStream;
import java.io.IOException;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.utils.Tools;

public class ChatItem
{
	public static final byte TYPE_QUAN = -1;// 全
	public static final byte TYPE_XI = 0;// 系
	public static final byte TYPE_SI = 1;// 私
	public static final byte TYPE_TEAM = 2;// 队
//	public static final byte TYPE_ZU = 3;// 家族
	public static final byte TYPE_QU = 4;// 区
	public static final byte TYPE_WORLD = 5;// 世
	public static final byte TYPE_FAMILY = 6;// 家
	public static final byte TYPE_REALM = 7;// 阵营
	public static final byte TYPE_FALL=8; //掉落（战斗）
	
	public static final long ID_NONE = -1;//无效
	public static final long ID_SYSTEM = 0;//显示在滚动广告栏
	//
	public byte m_type; //类型
	public long m_id_sender;
	public String m_name_sender;
	public String m_content; //聊天的内容
	public String m_name_recver;
	public byte m_camp;  //灵隐 1  天机 2 //阵营
	
	// Var 试图相关变量
	private int m_color;
	public String[] m_contents;

	/**
	 * 创建由自己创建的私聊记录对象 发件人是自己 收件人是他人 id是收件人Id
	 * @param type
	 * @param id_sender
	 * @param name_sender
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemBySelf(byte type, String name_recver, String content, long id, byte zhenying,
			byte vip)
	{
		return createChatItem(type, id, DWGameManager.getInstance().m_role.m_name, name_recver, content, zhenying, vip);
	}

	/**
	 * 创建对世的聊天 2012-4-6 下午04:42:53 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToWorld(String content, byte zhenying, byte vip)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_WORLD, "【世】", content, ID_NONE, zhenying, vip);
	}

	/**
	 * 创建对世的聊天 2012-4-6 下午04:42:53 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToRealm(String content, byte zhenying, byte vip)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_REALM, "【阵】", content, ID_NONE, zhenying, vip);
	}

	/**
	 * 创建对家族的聊天 2012-4-6 下午04:42:29 venizeng
	 * 所有集合对自己的发言都是Id = -1
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToFamily(String content, byte zhenying, byte vip)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_FAMILY, "【家】", content, ID_NONE, zhenying, vip);
	}

	/**
	 * 创建对队伍的聊天 2012-4-6 下午04:42:41 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToTeam(String content, byte zhenying, byte vip)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_TEAM, "【队】", content, ID_NONE, zhenying, vip);
	}

	/**
	 * 创建对世的聊天 2012-4-6 下午04:42:53 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToQu(String content, byte zhenying, byte vip)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_QU, "【区】", content, ID_NONE, zhenying, vip);
	}

	/**
	 * 读取聊天数据
	 * @param type
	 * @param id_sender
	 * @param name_sender
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItem(byte type, long id, String name_sender, String name_recver,
			String content, byte zhenying, byte vip)
	{
		ChatItem chatItem = new ChatItem();
		chatItem.m_type = type;
		chatItem.m_id_sender = id;
		chatItem.m_name_sender = name_sender;
		chatItem.m_name_recver = name_recver;
		chatItem.m_camp=zhenying; //阵营	
		chatItem.m_content = changeContent(type, id, name_sender, name_recver, content, zhenying, vip);
		Tools.debugPrintln("chatItem.m_content=" + chatItem.m_content);
		return chatItem;
	}

	/**
	 * 读取聊天记录
	 * 
	 * @param dis
	 * @return
	 * @throws IOException
	 */
	public static final ChatItem readChatInfos(DataInputStream dis) throws IOException
	{
		byte type = dis.readByte();
		long id = dis.readLong();
		String name = dis.readUTF();
		byte zhenying = dis.readByte(); //阵营   对应图标18/19
		byte vip = dis.readByte(); //vip 对应图标20-25
		String content = dis.readUTF();
		return createChatItem(type, id, (name), DWGameManager.getInstance().m_role.m_name, content, zhenying, vip);
	}

	/**
	 * 在聊天栏中添加内容
	 * @param type
	 * @param name
	 * @param content
	 * @return
	 */
	private static final String changeContent(byte type, long id_sender, String name_sender, String name_recver,
			String content, byte zhenying, byte vip)
	{
		if (id_sender == ID_SYSTEM) {
			name_sender = "";
		}
		byte LINGYIN = 19; //灵隐 1
		byte TIANJI = 18; //天机 2
		byte zhengyingtubiao=18; //阵营图标 下标
		if (zhenying==1) {
			zhengyingtubiao=LINGYIN;
		}else if (zhenying==2){
			zhengyingtubiao=TIANJI;
		}
		StringBuffer stringBuffer = new StringBuffer();
		if (vip > 0) {
			switch (type) {
				case ChatItem.TYPE_SI:
					stringBuffer.append("<mark c=ee2075>").append("[私]").append("</mark>").append(
							"<pic i=" + (19 + vip) + "></pic>" + "<pic i=" + (zhengyingtubiao) + "></pic>").append(
							"<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_TEAM:
					stringBuffer.append("<mark c=05b50c>").append("[队]").append("</mark>").append(
							"<pic i=" + (19 + vip) + "></pic>" + "<pic i=" + (zhengyingtubiao) + "></pic>").append(
							"<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_QU:
					stringBuffer.append("<mark c=eaec64>").append("[区]").append("</mark>").append(
							"<pic i=" + (19 + vip) + "></pic>" + "<pic i=" + (zhengyingtubiao) + "></pic>").append(
							"<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_WORLD:
					stringBuffer.append("<mark c=eaec64>").append("[世]").append("</mark>").append(
							"<pic i=" + (19 + vip) + "></pic>" + "<pic i=" + (zhengyingtubiao) + "></pic>").append(
							"<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_XI:
				case ChatItem.TYPE_FALL:
					stringBuffer.append("<mark c=ff0000>").append("[系]").append("</mark>").append("<mark t=p,i=")
							.append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(
							content);
					break;
				case ChatItem.TYPE_FAMILY:
					stringBuffer.append("<mark c=ff6b00>").append("[族]").append("</mark>").append(
							"<pic i=" + (19 + vip) + "></pic>" + "<pic i=" + (zhengyingtubiao) + "></pic>").append(
							"<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_REALM:
					stringBuffer.append("<mark c=9c89fd>").append("[阵]").append("</mark>").append(
							"<pic i=" + (19 + vip) + "></pic>" + "<pic i=" + (zhengyingtubiao) + "></pic>").append(
							"<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
			}
		}
		else {
			switch (type) {
				case ChatItem.TYPE_SI:
					stringBuffer.append("<mark c=ee2075>").append("[私]").append("</mark>").append(
							"<pic i=" + (zhengyingtubiao) + "></pic>").append("<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_TEAM:
					stringBuffer.append("<mark c=05b50c>").append("[队]").append("</mark>").append(
							"<pic i=" + (zhengyingtubiao) + "></pic>").append("<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_QU:
					stringBuffer.append("<mark c=eaec64>").append("[区]").append("</mark>").append(
							"<pic i=" + (zhengyingtubiao) + "></pic>").append("<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_WORLD:
					stringBuffer.append("<mark c=eaec64>").append("[世]").append("</mark>").append(
							"<pic i=" + (zhengyingtubiao) + "></pic>").append("<mark t=p,i=").append(
							id_sender).append(">").append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_XI:
				case ChatItem.TYPE_FALL:
					stringBuffer.append("<mark c=ff0000>").append("[系]").append("</mark>").append("<mark t=p,i=")
							.append(id_sender).append(">").append(name_sender).append("</mark>").append(":").append(
							content);
					break;
				case ChatItem.TYPE_FAMILY:
					stringBuffer.append("<mark c=ff6b00>").append("[族]").append("</mark>").append(
							"<pic i=" + (zhengyingtubiao) + "></pic>").append("<mark t=p,i=").append(id_sender).append(">")
							.append(name_sender).append("</mark>").append(":").append(content);
					break;
				case ChatItem.TYPE_REALM:
					stringBuffer.append("<mark c=9c89fd>").append("[阵]").append("</mark>").append(
							"<pic i=" + (zhengyingtubiao) + "></pic>").append("<mark t=p,i=").append(id_sender).append(">")
							.append(name_sender).append("</mark>").append(":").append(content);
					break;
			}
		}
		return stringBuffer.toString();
	}

	private int getColor()
	{
		switch (m_type) {
			case ChatItem.TYPE_SI:
				return 0xffe748f1;
			case ChatItem.TYPE_TEAM:
				return 0xff9196ff;
			case ChatItem.TYPE_QU:
				return 0xfff5e9c4;
			case ChatItem.TYPE_WORLD:
				return 0xfff3c743;
			case ChatItem.TYPE_XI:
				return 0xfff3c743;
			case ChatItem.TYPE_FAMILY:
				return 0xff67d267;
			case ChatItem.TYPE_REALM:
				return 0xfff5e9c4;
			default:
				return 0xffe748f1;
		}
	}

}
